package com.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.dingqb.util.NetworkUtils;

public class BaseActivity extends FragmentActivity {
	private MyProgressDialog mLoadingDialog;

	protected AppApplication app;
	protected BroadcastReceiver netReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
				networkChanged(NetworkUtils.isNetworkAvailable(BaseActivity.this), NetworkUtils.isWifi(BaseActivity.this));
				if (!NetworkUtils.isNetworkAvailable(BaseActivity.this)) {
					Toast.makeText(BaseActivity.this, "网络不可用", Toast.LENGTH_LONG).show();
				}

			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		app = AppApplication.getIntance();
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);

		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		super.registerReceiver(netReceiver, filter);
	}

	/**
	 * 检查网络情况
	 * 
	 * @param available
	 * @param isWifi
	 */
	protected void networkChanged(boolean available, boolean isWifi) {
	}

	/**
	 * Toast
	 * 
	 * @param msg
	 */
	protected void toast(String msg) {
		Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_LONG).show();
	}

	/**
	 * 页面跳转
	 * 
	 * @param cls
	 *            目标页面
	 * @param data
	 *            传递的数据
	 */
	public void startActivity(Class<? extends BaseActivity> cls, Bundle data) {
		Intent intent = new Intent(this, cls);
		if (null != data)
			intent.putExtras(data);
		startActivity(intent);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (netReceiver != null) {
			super.unregisterReceiver(netReceiver);
		}
		// 结束Activity&从堆栈中移除
		AppManager.getAppManager().finishActivity(this);

		if (null != mLoadingDialog && mLoadingDialog.isShowing())
			mLoadingDialog.cancel();
	}

	/** 显示数据加载提示对话框 */
	public void showLoadingDialog(String loadingMsg) {
		if (null == mLoadingDialog) {
			mLoadingDialog = new MyProgressDialog(this);
			mLoadingDialog.setCancelable(true);
			mLoadingDialog.setCanceledOnTouchOutside(false);
			mLoadingDialog.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {

				}
			});
		}
		mLoadingDialog.setMessage(loadingMsg);
		if (!mLoadingDialog.isShowing())
			mLoadingDialog.show();
	}

	/** 显示数据加载提示对话框 */
	public void showLoadingDialog() {
		showLoadingDialog("");
	}

	/** 关闭数据加载提示对话框 */
	public void closeLoadingDialog() {
		if (null != mLoadingDialog)
			mLoadingDialog.dismiss();
	}

}
